﻿using LXD.StudnetMangement.Models;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace LXD.StudnetMangement
{
    public class Startup
    {
        private readonly IConfiguration _configuration;

        //依赖注入
        public Startup(IConfiguration configuration)
        {
            _configuration = configuration;
        }

        // 配置服务器 第三方的插件
        public void ConfigureServices(IServiceCollection services)
        {
           
            services.AddDbContextPool<AppDbContext>(  //重性能上来说比AddDbContext 要好
                 optionsAction: options => options.UseSqlServer(_configuration.GetConnectionString("StudentDBConnection"))
                );
            services.AddMvc();
            //依赖注入
            services.AddScoped<IStudentRepository, SqlStudentRepository>();
        }

        //Http 请求的管道
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Error"); //拦截我们异常
                app.UseStatusCodePagesWithReExecute("/Error/{0}");//处理没有的页面
            }

            #region 合并
            //DefaultFilesOptions defaultFilesOptions = new DefaultFilesOptions();
            //defaultFilesOptions.DefaultFileNames.Clear();
            //defaultFilesOptions.DefaultFileNames.Add("52abp.html");
            //app.UseDirectoryBrowser(); //用户查看文件夹的目录
            ////添加默认文件中间件 UseDeafaaltFiles 一定要在UseStaticFiles 的前面
            //app.UseDefaultFiles(defaultFilesOptions);
            ////index.html index.htm default.html default.htm

            ////添加静态文件中间件
            //app.UseStaticFiles();


            //FileServerOptions fileServerOptions = new FileServerOptions();
            //fileServerOptions.DefaultFilesOptions.DefaultFileNames.Clear();
            //fileServerOptions.DefaultFilesOptions.DefaultFileNames.Add("52abp.html");
            //// 合并 UseDefaultFiles 和  UseStaticFiles 中间件
            #endregion

            app.UseStaticFiles();
            //带有默认路由 MVC
            //app.UseMvcWithDefaultRoute();
            app.UseMvc(routes =>
            {
                routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}
